package edu.ccut.saturn.workflow.startWorkFlowModule.code;

import edu.ccut.saturn.component.DefaultComponent;
import edu.ccut.saturn.component.IParameterContext;
import edu.ccut.saturn.component.SaturnData;
import edu.ccut.saturn.operation.Operation;
import edu.ccut.saturn.workflow.common.Constants;
import edu.ccut.saturn.workflow.model.StartInfo;
import edu.ccut.saturn.workflow.model.WorkFlowInfo;

public class SetWorkFlowInstance extends DefaultComponent {
	@Override
	public void execute(IParameterContext parameterContext) throws Exception {
		SaturnData workFlowInstance = (SaturnData) parameterContext
				.getParameterValue(0);
		WorkFlowInfo workFlowInfo = (WorkFlowInfo) parameterContext
				.getParameterValue(1);
		// 获得开始节点信息
		StartInfo startInfo = workFlowInfo.getStartInfo();
//		int process_instance_id = WorkFlowTools.getTableId(
//				Constants.WF_PROCESS_INSTANCE_SEQ, parameterContext.getConnection());
//		workFlowInstance.set("process_instance_id", process_instance_id);
		workFlowInstance.setString("start_time", Operation.baseService().GetSystemTime()); //$NON-NLS-1$
		workFlowInstance.set("state", Constants.WORKFLOW_STATE_RUNNING); //$NON-NLS-1$
		Object priority = workFlowInstance.get("priority"); //$NON-NLS-1$
		if(priority==null||"".equals(priority)){ //$NON-NLS-1$
			workFlowInstance.set("priority", 80); //$NON-NLS-1$
		}
		String name = (String) workFlowInstance.get("name"); //$NON-NLS-1$
		if(name==null||"".equals(name)){ //$NON-NLS-1$
			workFlowInstance.set("name", Messages.getString("SetWorkFlowInstance.0")); //未命名工作流
		}
		String limit_number = startInfo.getTimeLimit();
		// 流程的时间限制（取得开始活动中的时间限制）
		if (limit_number != null && !"".equals(limit_number)) { //$NON-NLS-1$
			workFlowInstance.set("limit_number", Integer.parseInt(limit_number)); //$NON-NLS-1$

		} else {
			workFlowInstance.set("limit_number", 0); //$NON-NLS-1$
		}
		parameterContext.setParameter(0, workFlowInstance);
	}
}